<template>
  <div class="goods-comment">
    <div class="head" v-if="goodsCommentInfo">
      <div class="data">
        <!-- 评论信息得总结 -->
        <p><span>{{ goodsCommentInfo.salesCount }}</span><span>人购买</span></p>
        <p><span>{{ goodsCommentInfo.praisePercent }}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a href="javascript:;" v-for="(item, i) in goodsCommentInfo.tags" :key="i"
            :class="{ active: currTagIndex === i }" @click="changeTag(i)">{{ item.title }}（{{
                item.tagCount
            }}）</a>

        </div>
      </div>
    </div>
    <!-- 排序 -->
    <div class="sort">
      <span>排序：</span>
      <a @click="changeSort(null)" href="javascript:;" :class="{ active: reqParams.sortField === null }">默认</a>
      <a @click="changeSort('praiseCount')" href="javascript:;"
        :class="{ active: reqParams.sortField === 'praiseCount' }">最热</a>
      <a @click="changeSort('createTime')" href="javascript:;"
        :class="{ active: reqParams.sortField === 'createTime' }">最新</a>
    </div>
    <!-- 顾客评论 -->
    <!-- 列表 -->
    <div class="list" v-if="total">
      <div class="item" v-for="item in commonList" :key="item.id">
        <div class="user" v-if="item.member != null">
          <img :src="item.member.avatar" alt="">
          <span>{{ formatNickname(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="i in item.score" :key="i + '1'" class="iconfont icon-wjx01"></i>
            <i v-for="i in 5 - item.score" :key="i + '2'" class="iconfont icon-wjx02"></i>
            <span class="attr" v-if="item.member != null">{{ formatSpecs(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 使用图片预览组件 -->
          <GoodsCommentImage v-if="item.pictures" :pictures="item.pictures" />
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan" @click=""><i class="iconfont icon-dianzan"></i> {{ item.praiseCount }}</span>
          </div>
        </div>
      </div>
    </div>
    <!-- 无评论 -->
    <div class="none" v-else>
      <span class="img"></span>
      <span class="text">亲，没有评论哦，您可以留下你的反馈吗?</span>
    </div>
    <!-- 分页 -->
    <XtxPagination @current-change="changePager" :total="total" :current-page="reqParams.page" v-if="total">
    </XtxPagination>
  </div>
</template>
<script lang="ts" setup>
import GoodsCommentImage from './goods-comment-image.vue'
import { ref, reactive, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findCommentInfoByGoods, findCommentList } from '../../api/goods'
const route = useRoute()
const goodsCommentInfo = ref(null)
findCommentInfoByGoods(route.params.id).then((data: any) => {
  // type 的目的是将来点击可以区分点的是不是标签, 添加有图和全部评价标签
  data.result.tags.unshift({ type: 'img', title: '有图', tagCount: data.result.hasPictureCount })
  data.result.tags.unshift({ type: 'all', title: '全部评价', tagCount: data.result.evaluateCount })
  goodsCommentInfo.value = data.result
})
// 点击标签事件
const currTagIndex = ref(0)
const changeTag = (i) => {
  currTagIndex.value = i
  const commonInfo = goodsCommentInfo.value.tags[i]
  // 全部评价
  if (commonInfo.type === 'all') { reqParams.hasPicture = null, reqParams.tag === null }
  // 有图
  else if (commonInfo.type === 'img') { reqParams.hasPicture = true, reqParams.tag === null }
  // 标签
  else { reqParams.hasPicture = null, reqParams.tag = commonInfo.title }
  // 重置页码
  reqParams.page = 1
}
// 准备发起请评论请求数据 筛选条件准备
const reqParams = reactive({
  // 页码
  page: 1,
  // 条数
  pageSize: 10,
  // 是否有图
  hasPicture: null,
  // 标签
  tag: null,
  // 排序
  sortField: null
})
// 评论筛选事件
const changeSort = (filteren) => {
  reqParams.sortField = filteren
  // 重置页码
  reqParams.page = 1
}
// 存放评论列表数据
const commonList = ref([])
// 记录总条数
const total = ref(0)
// 监听reqParams的变化发起数据请求，要初始化
watch(reqParams, () => {
  findCommentList(route.params.id, reqParams).then((data: any) => {
    commonList.value = data.result.items
    total.value = data.result.counts
    // console.log(data.result);
  })
}, { immediate: true })

// 定义转换数据的函数（对应vue2.0的过滤器）
// 顾客选择的商品规格过滤
const formatSpecs = (specs) => {
  return specs.reduce((p, c) => `${p} ${c.name}：${c.nameValue}`, '').trim()
}
// 顾客名字匿名
const formatNickname = (nickname) => {
  return nickname.substr(0, 1) + '***' + nickname.substr(-1)
}
// 改变页码发起的数据请求
const changePager = (np) => {
  reqParams.page = np
}
</script>
<style  lang="less" scoped>
@import '../../assets/styles/common.less';
@import '../../assets/styles/variables.less';

.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;

    .data {
      width: 340px;
      display: flex;
      padding: 20px;

      p {
        flex: 1;
        text-align: center;

        span {
          display: block;

          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }

          &:last-child {
            color: #999;
          }
        }
      }
    }

    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;

      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }

      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;

        >a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;

          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }

          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }

  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;

    >span {
      margin-left: 20px;
    }

    >a {
      margin-left: 30px;

      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }

  .list {
    padding: 0 20px;

    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;

      .user {
        width: 160px;

        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }

        span {
          padding-left: 10px;
          color: #666;
        }
      }

      .body {
        flex: 1;

        .score {
          line-height: 40px;

          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }

          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }

      .text {
        color: #666;
        line-height: 24px;
      }

      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}

.none {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;

  .img {
    width: 200px;
    height: 134px;
    background: url(../../assets/images/none.png) no-repeat center / contain;
  }

  .text {
    color: #999;
    font-size: 16px;
  }
}
</style>